package org.smart4j.framework.helper;

import java.lang.reflect.Field;
import java.util.Map;

import org.smart4j.framework.annotation.Inject;
import org.smart4j.framework.util.ArrayUtil;
import org.smart4j.framework.util.CollectionUtil;
import org.smart4j.framework.util.ReflectionUtil;

/**
 * 依赖注入助手类
 * @author TW
 *
 */
public final class IocHelper {

		static{
			//获取所有的Bean类与Bean实例之间的映射关系
			Map<Class<?>,Object> beanMap = BeanHelper.getBeanMap();
			if(CollectionUtil.isNotEmpty(beanMap)){
				//遍历BeanMap
				for(Map.Entry<Class<?>, Object> beanEntry : beanMap.entrySet()){
					//从beanMap中获取实例与类
					Class<?> beanClass  = beanEntry.getKey();
					Object beanInstance = beanEntry.getValue();
					//获取Bean类定义的所有成员变量
					Field[] beanFields = beanClass.getFields();
					if(ArrayUtil.isNotEmpty(beanFields)){
						for(Field beanField : beanFields){
							//判断当前属性是否有Inject注解
							if(beanField.isAnnotationPresent(Inject.class)){
								//在bean Map中获取 bean field 对应的实例
								Class<?> beanFieldClass = beanField.getType();
								Object beanFieldInstance = beanMap.get(beanFieldClass);
								if(beanFieldInstance!=null){
									//通过反射初始化BeanField的值
									ReflectionUtil.setField(beanInstance, beanField, beanFieldInstance);
								}
							}
						}
					}
				}
			}
			
		} 
}
